package thinkingJava.chapter11;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Set;

public class PriorityQueueDemo {
	public static void main(String[] args) {
		PriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>();
		Random rand = new Random(47);
		for (int i = 0; i < 10; i ++) {
			priorityQueue.offer(rand.nextInt(i + 10));
		}
		printQ(priorityQueue);
		List<Integer> ints = Arrays.asList(23,28,30,25,1,0,3,2,3,40,5,22,21,2,4,5,7,9,21,15,26);
		priorityQueue = new PriorityQueue<Integer>(ints);
		printQ(priorityQueue);
		priorityQueue = new PriorityQueue<Integer>(ints.size(),Collections.reverseOrder());
		priorityQueue.addAll(ints);
		printQ(priorityQueue);

		String fact = "EDUCATION SHOULD ESCHEW OBFUSCATION";
		List<String> strings = Arrays.asList(fact.split(""));
		PriorityQueue<String> stringQ = new PriorityQueue<String>(strings);
		print(stringQ);
		stringQ = new PriorityQueue<String>(strings.size(),Collections.reverseOrder());
		stringQ.addAll(strings);
		print(stringQ);
		Set<Character> charSet = new HashSet<Character>();
		for (char c : fact.toCharArray()) {
			charSet.add(c);
		}
		PriorityQueue<Character> charPQ = new PriorityQueue<Character>(charSet);
		print(charPQ);
		charPQ = new PriorityQueue<Character>(charSet.size(),Collections.reverseOrder());
		charPQ.addAll(charSet);
		print(charPQ);
	}
	public static void print(Object o) {
		System.out.println(o);
	}
	public static void printQ(Queue q) {
		while (q.peek() != null) {
			System.out.print(q.remove() + "    ");
		}
		System.out.println();
	}
}
